const Indexed = (function() {
  function Indexed() {
    this.database = 't'
    this.version = 1
    this.registeredDB = []
  }
  Object.defineProperty(Indexed, 'instance', {
    get: function() {
      if (!Indexed.instant) {
        Indexed.instant = new Indexed()
      }
      return Indexed.instant
    },
    enumerable: false,
    configurable: true
  })
  Indexed.register = function(db) {
    Indexed.instance.registeredDB.push(db)
  }
  Indexed.install = function() {
    Indexed.instance.link().then()
  }
  Indexed.prototype.link = function() {
    const _this = this
    return new Promise(function(resolve, reject) {
      if (Indexed.instance.db) {
        resolve()
      }
      _this.requestInstance = indexedDB.open(_this.database, _this.version)
      _this.requestInstance.onsuccess = _this.onLinkSuccess(resolve)
      _this.requestInstance.onupgradeneeded = _this.onLinkUpgradeNeeded(resolve)
      _this.requestInstance.onerror = _this.onError(reject)
    })
  }
  Indexed.prototype.onLinkSuccess = function(resolve) {
    return function(event) {
      Indexed.instance.db = event.target.result
      console.log('success')
      resolve()
    }
  }
  Indexed.prototype.onLinkUpgradeNeeded = function(resolve) {
    return function(event) {
      const db = Indexed.instance.db = event.target.result
      for (let _i = 0, _a = Indexed.instance.registeredDB; _i < _a.length; _i++) {
        const table = _a[_i]
        if (!db.objectStoreNames.contains(table.table)) {
          table.handle(db)
        } else {
          console.error(table.table + '\u8868\u5DF2\u5B58\u5728')
        }
      }
      resolve()
    }
  }
  Indexed.prototype.onError = function(reject) {
    return function(event) {
      reject(event.target.error)
    }
  }
  return Indexed
}())
const Model = (function() {
  function Model() {
  }
  Model.prototype.link = function() {
    return Indexed.instance.link()
  }
  Model.prototype.create = function(data) {
    const _this = this
    this.link().then(function(_) {
      _this.add(data)
    }).catch(function(e) {
      console.log(e)
    })
  }
  Model.prototype.add = function(data, success, error) {
    let request
    if (data.length > 0) {
      request = Indexed.instance.db.transaction([this.table], 'readwrite')
      const objStore_1 = request.objectStore(this.table)
      data.forEach(function(item) {
        objStore_1.add(item)
      })
    } else {
      request = Indexed.instance.db.transaction([this.table], 'readwrite')
        .objectStore(this.table)
        .add(data)
    }
    request.onsuccess = function(event) {
      console.log('数据写入成功')
      if (success) {
        success(event)
      }
    }
    request.onerror = function(event) {
      console.log('数据写入失败')
      if (error) {
        error(event)
      }
    }
  }
  Model.prototype.find = function(id) {
    const _this = this
    return new Promise(function(resolve, reject) {
      _this.link().then(function(_) {
        _this.read(id, resolve, reject)
      }).catch(function(e) {
        reject(e)
      })
    })
  }
  Model.prototype.read = function(id, resolve, reject) {
    const request = Indexed.instance.db.transaction([this.table])
      .objectStore(this.table)
      .get(id)
    request.onerror = function(event) {
      console.log('事务失败', event)
    }
    request.onsuccess = function(event) {
      console.log(event)
      if (request.result) {
        resolve(request.result)
      } else {
        reject()
      }
    }
  }
  Model.prototype.delete = function(id) {
    const _this = this
    return new Promise(function(resolve, reject) {
      _this.link().then(function(_) {
        _this.del(id, resolve, reject)
      }).catch(function(e) {
        reject(e)
      })
    })
  }
  Model.prototype.del = function(id, resolve, reject) {
    const request = Indexed.instance.db.transaction([this.table], 'readwrite')
      .objectStore(this.table)
      .delete(id)
    request.onerror = function(event) {
      console.log('事务失败')
      reject(event)
    }
    request.onsuccess = function(event) {
      resolve(event)
    }
  }
  Model.prototype.all = function() {
    const _this = this
    return new Promise(function(resolve, reject) {
      _this.link().then(function(_) {
        _this.openCursor(resolve, reject)
      }).catch(function(e) {
        reject(e)
      })
    })
  }
  Model.prototype.in = function(arr) {
    const _this = this
    return new Promise(function(resolve, reject) {
      _this.link().then(function(_) {
        _this.openCursor(resolve, reject, arr)
      }).catch(function(e) {
        reject(e)
      })
    })
  }
  Model.prototype.openCursor = function(resolve, reject, inArr) {
    const request = Indexed.instance.db.transaction([this.table])
    let objectStore, keyRange
    if (this.condition) {
      objectStore = request.objectStore(this.table).index(this.condition.column)
      keyRange = IDBKeyRange.only(this.condition.condition)
    } else {
      objectStore = request.objectStore(this.table)
    }
    const v = objectStore.openCursor(keyRange)
    v.onerror = function(event) {
      console.log('事务失败')
      reject(event)
    }
    const res = []
    v.onsuccess = function(event) {
      const cursor = event.target.result
      if (cursor) {
        if (inArr) {
          if (inArr.indexOf(cursor.value.id) > -1) {
            res.push(cursor.value)
          }
        } else {
          res.push(cursor.value)
        }
        cursor.continue()
      } else {
        resolve(res)
      }
    }
  }
  Model.prototype.where = function(column, condition) {
    this.condition = { column: column, condition: condition }
    return this
  }
  return Model
}())
export { Indexed, Model }
